<?php

namespace App\Http\Resources\Frontend;

use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Resources\Json\JsonResource;

class Resource extends JsonResource
{
    public $with = [
        'code' => 200,
        'message' => 'OK'
    ];

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }

    static function collection($resource)
    {
        $collection = new AnonymousResourceCollection($resource, static::class);
        $collection->with = (new static([]))->with;
        return tap($collection, function ($collection) {
            if (property_exists(static::class, 'preserveKeys')) {
                $collection->preserveKeys = (new static([]))->preserveKeys === true;
            }
        });
    }
}
